package com.westa.smallshop.shop;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.westa.smallshop.R;
import com.westa.smallshop.cart.CartActivity;
import com.westa.smallshop.common.BaseListAdapter;
import com.westa.smallshop.common.BaseListFragment;
import com.westa.smallshop.common.BasePresenter;
import com.westa.smallshop.common.GetPresenterInterface;
import com.westa.smallshop.common.RecyclerViewBaseAdapter;
import com.westa.smallshop.data.GoodsItem;
import com.westa.smallshop.goodsdetail.GoodsDetailActivity;
import com.westa.smallshop.goodssearch.GoodsSearchActivity;
import com.westa.smallshop.util.DensityUtils;
import com.westa.smallshop.util.ListUtils;
import com.westa.smallshop.widget.recyclerview.HeaderAndFooterRecyclerViewAdapter;
import com.westa.smallshop.widget.recyclerview.RecyclerViewUtils;

import java.util.List;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.bgabanner.BGABanner;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by cy on 10/26/16.
 */

public class ShopFragment extends BaseListFragment<ShopContract.Presenter, GoodsItem>
        implements ShopContract.View<GoodsItem>, RecyclerViewBaseAdapter.OnItemClickListener, GetPresenterInterface, BGABanner.Adapter {

    @BindString(R.string.title_shop)
    String mTitle;
    @NonNull
    private ShopContract.Presenter mPresenter;
    private ShopAdapter mAdapter;
    private PagerHolder mPagerHolder;

    public static ShopFragment newInstance() {
        return new ShopFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @NonNull
    @Override
    public BaseListAdapter getAdapter() {
        if (mAdapter == null) {
            mAdapter = new ShopAdapter(this);
            mAdapter.setOnItemClickListener(this);
        }
        return mAdapter;
    }

    @Override
    public int getCreateViewLayoutRes() {
        return R.layout.fragment_shop;
    }

    @Override
    public void initView() {
        super.initView();
        getActivity().setTitle(mTitle);
        setHasOptionsMenu(true);

        RecyclerView recyclerView = getRecyclerView();
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        HeaderAndFooterRecyclerViewAdapter adapter = new HeaderAndFooterRecyclerViewAdapter(mAdapter);
        recyclerView.setAdapter(adapter);
        mPagerHolder = new PagerHolder(getActivity().getLayoutInflater().inflate(R.layout.view_homepage_pager, getRecyclerView(), false), this);
        RecyclerViewUtils.setHeaderView(recyclerView, mPagerHolder.getView());
        final int m = DensityUtils.dp2px(getContext(), 3.0f);
        getRecyclerView().addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.set(m, m, m, m);
            }
        });

        mPagerHolder.mBannerHomepagePager.setAdapter(this);
        mPagerHolder.mBannerHomepagePager.setAutoPlayAble(true);
        mPagerHolder.mBannerHomepagePager.setData(((ShopPresenter) getPresenter()).getPagerImageUrl(), null);

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            getActivity().setTitle(mTitle);
        }
    }

    @Override
    public void showRefreshSuccessful(List<GoodsItem> goodsItems) {
        mAdapter.refreshData(goodsItems);
    }

    @Override
    public void onStart() {
        super.onStart();
        mPresenter.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.destroy();
    }

    @Override
    public void refreshData() {
        mPresenter.refreshData();
    }

    @Override
    public void loadMoreData(int currentPage) {
        mPresenter.loadMoreData(String.valueOf(currentPage));
    }

    @Override
    public void refreshData(List<GoodsItem> list) {
        if (!ListUtils.isEmpty(list)) {
            mAdapter.refreshData(list);
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.shop_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_search:
                GoodsSearchActivity.startActivity(getActivity());
                break;
        }
        return true;
    }

    @Override
    public void loadMoreData(List<GoodsItem> list) {
        mAdapter.loadMoreData(list);
    }

    @Override
    public void getDataFailed(String errorMsg) {

    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void setPresenter(ShopContract.Presenter presenter) {
        mPresenter = checkNotNull(presenter);
    }

    @Override
    public void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        GoodsDetailActivity.startActivity(getActivity(), mAdapter.getItemObject(position).getGoods_id());
    }

    @Override
    public void fillBannerItem(BGABanner banner, View view, Object model, int position) {
        Glide.with(this)
                .load(model)
                .centerCrop()
                .into((ImageView) view);
    }

    @OnClick(R.id.fab_cart)
    public void onClick() {
        CartActivity.startActivity(getActivity());
    }

    class PagerHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.banner_homepage_pager)
        BGABanner mBannerHomepagePager;
        View mView;
        private ShopFragment mFragment;

        public PagerHolder(View itemView, final ShopFragment fragment) {
            super(itemView);
            mView = itemView;
            mFragment = fragment;
            ButterKnife.bind(this, itemView);
        }

        public View getView() {
            return mView;
        }

    }

}
